package offer;

public class offer55second {
    public boolean isBalanced(TreeNode root) {
        if(root==null) return true;
        int left = maxDeep(root.left);
        int right = maxDeep(root.right);
        if(Math.abs(left-right)>1)
            return false;
        boolean l  = isBalanced(root.left);
        boolean r = isBalanced(root.right);
        return l&&r;
    }

    private int maxDeep(TreeNode root) {
        if(root==null) return 0;
        int left  = maxDeep(root.left);
        int right = maxDeep(root.right);

        return Math.max(left,right)+1;
    }
}
